import util.SortCompare;
import util.SuperArray;

import javax.swing.*;

/**
 * 斗地主发牌界面
 */
public class DealPukeUI {
    private SuperArray pukeAry = new SuperArray();
    public void process(){
        this.init();
        this.shuffle();
        this.deal();
    }
    /**
     * 初始化
     */
    public void init(){
        String [] points = {"3","4","5","6","7","8","9","10","J","Q","K","A","2"};
        String [] types = {"黑桃","红心","梅花","方块"};
        for(int i = 0;i < points.length;i++){
            for(int j = 0;j < types.length;j++){
                pukeAry.add(new Puke(points[i],types[j],i,j));
            }
        }
        pukeAry.add(new Puke("大王",14));
        pukeAry.add(new Puke("小王",13));
    }

    /**
     * 洗牌
     */
    public void shuffle(){
        for(int i = 0;i < pukeAry.size();i++){
            int index = (int)(Math.random() * pukeAry.size());
            Object temp = pukeAry.get(i);
            pukeAry.set(pukeAry.get(index),i);
            pukeAry.set(temp,index);
        }
    }

    /**
     * 发牌
     */
    public void deal(){
        // 给玩家一发牌
        String player1 = generatePukeInfo(0,17);
        // 给玩家二发牌
        String player2 = generatePukeInfo(17,34);
        // 给玩家三发牌
        String player3 = generatePukeInfo(34,51);
        // 底牌
        String base = generatePukeInfo(51,54);
        JOptionPane.showMessageDialog(null,
                "玩家一：" + player1 + "\n" +
                "玩家二：" + player2 + "\n" +
                "玩家三：" + player3 + "\n" +
                "底牌：" + base);

    }

    public String generatePukeInfo(int start,int end){
        String pukeStr = "";
        SuperArray ary = new SuperArray();
        for(int i = start;i < end;i++){
            ary.add(pukeAry.get(i));
        }
        sort(ary);
        for(int i = 0;i < ary.size();i++){
            pukeStr += ary.get(i)+",";
        }
        return pukeStr;
    }
    /**
     * 排序
     */
    public void sort(SuperArray ary){
        ary.sort(new SortCompare() {
            @Override
            public int compare(Object prev, Object next) {
                Puke prevPuke = (Puke)prev;
                Puke nextPuke = (Puke)next;
                if(prevPuke.getPointValue() < nextPuke.getPointValue()){
                    return 1;
                }else if(prevPuke.getPointValue() == nextPuke.getPointValue()){
                    if(prevPuke.getTypeValue() > nextPuke.getTypeValue()){
                        return 1;
                    }else{
                        return -1;
                    }
                }else{
                    return -1;
                }
            }
        });
//        for(int i = 0;i < ary.size() - 1;i++){
//            for(int j = 0;j < ary.size() - 1 - i;j++){
//                Puke prevPuke = (Puke)ary.get(j);
//                Puke nextPuke = (Puke)ary.get(j+1);
//                if(prevPuke.getPointValue() < nextPuke.getPointValue()){
//                    Object temp = prevPuke;
//                    ary.set(nextPuke,j);
//                    ary.set(temp,j+1);
//                }else if(prevPuke.getPointValue() == nextPuke.getPointValue()){
//                    if(prevPuke.getTypeValue() > nextPuke.getTypeValue()){
//                        Object temp = prevPuke;
//                        ary.set(nextPuke,j);
//                        ary.set(temp,j+1);
//                    }
//                }
//            }
//        }
    }
}
